package com.taimi.newdemo.config;

import com.taimi.newdemo.entity.UserEntity;
import com.taimi.newdemo.service.UserService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;

@Configuration(proxyBeanMethods = false)
public class SystemConfiguration {

    @Autowired
    private UserService userService;

    @Bean
    public CommandLineRunner createDefaultAccount(Environment env){
        return args -> {
            String account = env.getProperty("OPENVPN_USER","admin");
            UserEntity user = userService.findUserByUsername(account);
            if (user == null) {                
                String password = env.getProperty("OPENVPN_PASSWORD","admin");
                user = UserEntity.builder().username(account).password(password).build();
                userService.saveUser(user);
            }
        };
    }
}
